package com.example.administrator.energyida.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.energyida.R;
import com.example.administrator.energyida.login.LoginActivity;
import com.example.administrator.energyida.util.OceanUtil;
import com.jaeger.library.StatusBarUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class MyinfoRevisepasswordActivity extends Activity {

    private EditText oldPassword;
    private EditText newPassword;
    private EditText repeatNew;
    private Button btnSubmit;
    private TextView textViewBack;

    private Handler handler;
    private String response;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myinfo_revisepassword);

        // 设置状态栏的颜色
        StatusBarUtil.setColor(this, Color.parseColor("#10cafd"), 50);

        SharedPreferences spAccount = getSharedPreferences("account", MODE_PRIVATE);
        String email = spAccount.getString("email", "");
        String password = spAccount.getString("password", "");

        oldPassword = findViewById(R.id.revisepw_editText_oldPw);
        newPassword = findViewById(R.id.revisepw_editText_newPw);
        repeatNew = findViewById(R.id.revisepw_editText_repeat);
        btnSubmit = findViewById(R.id.revisepw_btn_submit);
        textViewBack = findViewById(R.id.text_revisepw_back);

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (response.contains("10007")) {
                    Toast.makeText(MyinfoRevisepasswordActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();

                    // 清空记住密码和自动登录的SharedPreferences中的数据
                    SharedPreferences sharedPreferences = getSharedPreferences("remember", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.clear();
                    editor.commit();
                    SharedPreferences sharedPreferences2 = getSharedPreferences("auto", MODE_PRIVATE);
                    SharedPreferences.Editor editor2 = sharedPreferences.edit();
                    editor2.clear();
                    editor2.commit();

                    //重新登录
                    Intent intent = new Intent(MyinfoRevisepasswordActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                if (response.contains("-20007")) {
                    Toast.makeText(MyinfoRevisepasswordActivity.this, "密码修改失败", Toast.LENGTH_SHORT).show();
                    oldPassword.setText("");
                    newPassword.setText("");
                    repeatNew.setText("");
                }
            }
        };

        oldPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    String oldpwd = oldPassword.getText().toString().trim();
                    if (oldpwd.equals(password)) {
                        Toast.makeText(MyinfoRevisepasswordActivity.this, " 原密码输入正确", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MyinfoRevisepasswordActivity.this, "原密码输入错误", Toast.LENGTH_SHORT).show();
                        oldPassword.setText("");
                    }
                }
            }
        });

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPw = oldPassword.getText().toString();
                String newPw = newPassword.getText().toString();
                String reNew = repeatNew.getText().toString();

                if (oldPw.equals("")) {
                    Toast.makeText(MyinfoRevisepasswordActivity.this, "旧密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (newPw.equals(reNew) == false) {
                    Toast.makeText(MyinfoRevisepasswordActivity.this, "新密码输入不一致，请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }

                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("email", email);
                    jsonObject.put("newpwd", newPw);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                new OceanUtil("resetpwd.php", jsonObject, new OceanUtil.HttpRequestCallback() {
                    @Override
                    public void callback(String strResponse) {
                        response = strResponse;
                        Message message = new Message();
                        handler.sendMessage(message);
                    }
                }).postInfo();
            }
        });

        textViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
